package framework.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import framework.cellularAutomaton.CellularAutomaton;

@SuppressWarnings("serial")
public class TopButtonPanel extends JPanel implements ActionListener{
	
	private JButton toggleGrid;
	private JButton clear;
	private JComboBox<String> statePicker;
	
	private GridPanel gridPanel;
	
	private CellularAutomaton controller;
	
	/**
	 * Initializes and sets up the top panel of buttons that
	 * the user will be interacting with, as well as the 
	 * state picker for the paint feature.
	 * 
	 * @param listener the GridPanel that will be listening 
	 * to the buttons and the state picker.
	 */
	
	public TopButtonPanel(GridPanel listener, CellularAutomaton CA) {
		gridPanel = listener;
		controller = CA;
		
		//Set the layout of the panel
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		addStateMenu();
		
		//Initialize the buttons
		toggleGrid = new JButton("Hide Grid");
		clear = new JButton("Reset Cells");
		
		//Give each button its action command
		toggleGrid.setActionCommand("toggle");
		clear.setActionCommand("reset");
		
		//Set each button's listener (the GridPanel)
		clear.addActionListener(listener);
		toggleGrid.addActionListener(listener);
		toggleGrid.addActionListener(this);
		
		//Add the button to the panel
		add(clear);
		add(toggleGrid);
	}

	/**
	 * Adds the state picker if there are enough states
	 * defined by the RuleSet to warrant one. Takes the names of 
	 * the states from the getStateName function in RuleSet.
	 */
	
	public void addStateMenu(){
		if(gridPanel.getNumStates() > 2){
			JLabel pickerLabel = new JLabel("Pick a state to paint in: ");
		
			String[] states = new String[gridPanel.getNumStates()];
			String[] stateNames = controller.getWorld().getRules().getStateNames();
			
			if(stateNames != null){
				for(int i = 0; i < gridPanel.getNumStates(); i++){
					if(i >= stateNames.length || stateNames[i] == null){
						states[i] = i + "";
					}
					else{
						states[i] = stateNames[i];
					}
				}
			}
			else{
				for(int i = 0; i < gridPanel.getNumStates(); i++){
					states[i] = i + "";
				}
			}
			
			statePicker = new JComboBox<String>(states);
			
			pickerLabel.setLabelFor(statePicker);
			statePicker.setSelectedIndex(gridPanel.getDefaultState());
			statePicker.addActionListener(this);
			statePicker.setEditable(true);
			
			add(pickerLabel);
			add(statePicker);
		}
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("toggle")){
			if("Hide Grid".equals(toggleGrid.getText())){
				toggleGrid.setText("Show Grid");
			}
			else{
				toggleGrid.setText("Hide Grid");
			}
			repaint();
		}
		else{
	        @SuppressWarnings("unchecked")
			JComboBox<String> tmp = (JComboBox<String>)arg0.getSource();
	        int selectionIndex = tmp.getSelectedIndex();
	        gridPanel.setPaintState(selectionIndex);
		}
	}
}
